package ru.hh.team02.chat.client;

import ru.hh.team02.chat.database.DatabaseServiceDerby;
import ru.hh.team02.chat.database.DatabaseServiceInterface;
import ru.hh.team02.chat.server.Services;

/**
 * Class with methods to handle messages.
 */
public class MessageHandler {
    private String clientName;
    public DatabaseServiceInterface dbService;

    /**
     *
     * @param clientName Client name the messages are addressed to.
     */
    public MessageHandler(String clientName, DatabaseServiceInterface dbService) {
        this.clientName = clientName;
        this.dbService = dbService;
    }

    /**
     * Mark message in the DB as delivered or delete (depends on implementation)
     * for it to not longer be sent to the user on their login.
     * @param messageId Unparsed message id.
     */
    public void markMessageInDb(String messageId) {
        int id = 0;
        try {
            id = Integer.parseInt(messageId);
            dbService.markPersonalMessageAsDelivered(id);
        } catch (NumberFormatException e) {
            Services.ADMIN_LOGGER.write("Problems with parsing incoming message. Something went wrong.");
            e.printStackTrace();
        }
    }

    /**
     * Handle message to change room.
     * @param message Unparsed message.
     */
    public void changeRoom(String message) {
        String[] info = message.split(" ");
        if (info.length == 2) {
            String roomName = info[1];

            if (Services.registry.changeRoom(clientName, roomName)) {
                dbService.changeRoom(clientName, roomName);

                Services.messenger.sendServiceMessage(String.format("Welcome to %s!", roomName), clientName);
                Services.ADMIN_LOGGER.write("Client \"" + clientName
                        + "\" entered in new room \"" + roomName + "\"");
            } else {
                Services.messenger.sendServiceMessage(String.format(
                        "server> no such room%n" +
                                "\"Possible rooms are: %s.", Services.registry.getRoomNames().toString()),
                        clientName);
                Services.ADMIN_LOGGER.write("Client \"" + clientName
                        + "\" tried to switch to nonexistent room");
            }
        }
    }

    /**
     * Send personal message to the client specified inside the message.
     * @param message Unparsed private message.
     */

    public void sendPersonalMessage(String message) {
        String[] info = message.split(" ", 3);
        if (info.length == 3) {
            String recepientName = info[1];
            String messageText = info[2];


            Services.messenger.sendPersonalMessage(
                    "personal:" + clientName + "> " + messageText,
                    recepientName,
                    clientName, dbService);
        }
    }

    /**
     * Send the message to the room the client this handler belongs to is in.
     * @param message Message to send.
     */
    public void sendMessageToRoom(String message) {
        Services.messenger.sendMessageToRoom(clientName + "> " + message,
                clientName);
        Services.ADMIN_LOGGER.write("Client \"" + clientName
                + "\" sent new message: \"" + message + "\"");
    }

}
